package com.daoliuhe.scaffold.web.controller.admin;

import com.daoliuhe.scaffold.model.UserRoleRelation;
import com.daoliuhe.scaffold.service.UserRoleRelationService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;

@Controller
@RequestMapping("/userRoleRelation")
public class UserRoleRelationController {

    private static final Logger logger = LoggerFactory.getLogger(UserRoleRelationController.class);
    @Resource
    UserRoleRelationService userRoleRelationService;

    /**
     * 根据用户查找角色
     *
     * @param entity
     * @return
     */
    @RequestMapping("/roleData")
    @ResponseBody
    public Object roleData(UserRoleRelation entity) {
        logger.info("roleData,entity:{}", entity);
        return userRoleRelationService.getRolePageData(entity);
    }

    @RequestMapping("/userData")
    @ResponseBody
    public Object userData(UserRoleRelation entity) {
        logger.info("userData,entity:{}", entity);
        return userRoleRelationService.getUserPageData(entity);
    }

    /**
     * 保存用户角色关系
     *
     * @param entity
     * @return
     */
    @RequestMapping("/save")
    @ResponseBody
    public Object saveRelation(UserRoleRelation entity) {
        logger.info("saveRelation,entity:{}", entity);
        return userRoleRelationService.saveRelation(entity);
    }

    /**
     * 删除用户角色的关系
     *
     * @param userId
     * @param roleIds
     * @return
     */
    @RequestMapping("/deleteForUser")
    @ResponseBody
    public Object deleteForUser(String userId, String roleIds) {
        logger.info("deleteForUser,userId:{},roleIds:{}", userId, roleIds);
        return userRoleRelationService.deleteForUser(userId, roleIds);
    }

    @RequestMapping("/deleteForRole")
    @ResponseBody
    public Object deleteForRole(String userIds, String roleId) {
        logger.info("deleteForRole,userIds:{},roleId:{}", userIds, roleId);
        return userRoleRelationService.deleteForRole(userIds, roleId);
    }

    /**
     * 根据角色批量导入用户
     *
     * @param userLoginIds
     * @param roleId
     * @return
     */
    @RequestMapping("/saveBatchUser")
    @ResponseBody
    public Object saveBatchUser(String userLoginIds, String roleId) {
        logger.info("saveBatchUser,userLoginIds:{},roleId:{}", userLoginIds, roleId);
        return userRoleRelationService.saveBatchUser(userLoginIds, roleId);
    }

    /**
     * 根据用户批量添加角色
     *
     * @param userId
     * @param roleIds
     * @return
     */
    @RequestMapping("/saveBatchRole")
    @ResponseBody
    public Object saveBatchRole(String userId, String roleIds) {
        logger.info("saveBatchRole,userId:{},roleIds:{}", userId, roleIds);
        return userRoleRelationService.saveBatchRole(userId, roleIds);
    }

    /**
     * 根据角色批量添加用户
     *
     * @param userIds
     * @param roleId
     * @return
     */
    @RequestMapping("/saveBatchUserForRole")
    @ResponseBody
    public Object saveBatchUserForRole(String userIds, String roleId) {
        logger.info("saveBatchUserForRole,userIds:{},roleId:{}", userIds, roleId);
        return userRoleRelationService.saveBatchUserForRole(userIds, roleId);
    }

}
